
from pts.models import AppEXCD,VerCD,VerBase,Submit
from pts.nodes.app import AppNodeCD,app_created
from pts.nodes.versin import VersionCD

def say_cd(**kw):
    print('cd here')

def say_nj(**kw):
    print('NJ here')  

def make_flow(app):
    if app.flow=='FlowCD':
        return FlowCD(app)
    elif app.flow=='FlowNJ':
        return FlowNJ(app)

class FlowCD(object):
    app_created.connect(say_cd, sender='FlowCD')
    
    def __init__(self,app):
        self.app=app
    
    def get_nodes(self):
        return [AppNodeCD,'appversion',set('validation','dev'),'rel','submit']
    
    def add_version(self,vercode):
        VersionCD.create(self.app,vercode)
    
    def add_qa(self,vercode):
        ver=VerBase.objects.get(app=self.app,vercode=vercode).vercd
        ver.dev_code='dev_OK'
        ver.save()
    
    def add_rel_qa(self,vercode):
        ver=VerBase.objects.get(app=self.app,vercode=vercode).vercd
        ver.rel_code='rel_OK'
        ver.save()        
    
    def add_sub(self,vercode):
        verbase=VerBase.objects.get(app=self.app,vercode=vercode)
        Submit.objects.create(version=verbase,sub_code='SUB_OK')
    
    def get_show(self):
        return {'app':self.get_app(),
                'version':self.get_version(),
                'flow_type':'FlowCD'
                }
    
    def get_app(self):
        return self.app.appcode
    
    def get_version(self):
        out=[]
        for ver in self.app.verbase_set.all():
            out.append({'vercode':ver.vercode,
                        'dev_qa':self.get_dev_qa(ver.vercd),
                        'rel_qa':self.get_rel_qa(ver.vercd),
                        'subs':self.get_submit(ver)}
                       )
        return out
    
    def get_dev_qa(self,version):
        return version.dev_code
    
    def get_rel_qa(self,version):
        return version.rel_code
    
    def get_submit(self,version):
        return [sub.sub_code for sub in version.submit_set.all()]
    
class FlowNJ(object):
    app_created.connect(say_nj, sender='FlowNJ')
    def __init__(self,app):
        self.app=app
    
    def get_nodes(self):
        return [AppNodeCD,'appversion',set('validation','dev'),'rel','submit']
    
    def get_show(self):
        return {'app':self.get_app(),
                'version':self.get_version(),
                'flow_type':'FlowNJ'
                }
    
    def get_app(self):
        return self.app.appcode
    
    def get_version(self):
        return 'no version'
    
    def add_version(self,vercode):
        VersionCD.create(self.app,vercode)    
    # def proc_event(self):
        
    # @classmethod
    # def say(cls):
        # print('NJ here')    
    

# class VerCDFlow(object):
    
    

